home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / RandomDot 1.1.0 / source / RandomDotWinCommands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-20  |  6.6 KB  |  268 lines  |  [TEXT/KAHL]

  1. /* RandomDotWinCommands.c
  2.     by David Phillip Oster October 1994 oster@netcom.com
  3.     for:
  4.     Stuart Inglis singlis@waikato.ac.nz
  5.     Department of Computer Science
  6.     University of Waikato, Hamilton, New Zealand
  7.  */
  8. #include "RandomDotMain.h"
  9. #include "RandomDotRes.h"
  10.  
  11. #include "RandomDotWinCommands.h"
  12. #include "Error.h"
  13. #include "RandomDot.h"
  14. #include "RandomDotWin.h"
  15. #include "Menu.h"
  16. #include "Utils.h"
  17.  
  18. /* DoRandomDotSave - 
  19.  */
  20. void DoRandomDotSave(void){
  21.     RandomDotWindowPtr    win;
  22.  
  23.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  24.         SetPort((WindowPtr) win);
  25.         if(NOT (bSaved & win->flags)){
  26.             DoRandomDotSaveAs();
  27.         }
  28.     }
  29. }
  30.  
  31. /* RandomDotPicHandle - return whole window as a pict.
  32.  */
  33. static PicHandle RandomDotPicHandle(RandomDotWindowPtr win){
  34.     RgnHandle    saveClip;
  35.     PicHandle    ph;
  36.  
  37.     saveClip = NewRgn();
  38.     GetClip(saveClip);
  39.     ClipRect(&win->frame);
  40.     ph = OpenPicture(&win->frame);
  41.     RandomDotCopybitsWin(win, &win->frame);
  42.     ClosePicture();
  43.     SetClip(saveClip);
  44.     DisposeRgn(saveClip);
  45.     return ph;
  46. }
  47.  
  48. /* DoRandomDotSaveAs - 
  49.  */
  50. void DoRandomDotSaveAs(void){
  51.     OSErr                errCode, modCode;
  52.     Integer                ref;
  53.     LongInt                len;
  54.     RandomDotWindowPtr    win;
  55.     StandardFileReply    reply;
  56.     Str255                s;
  57.     PicHandle            ph;
  58.     FInfo                fInfo;
  59.  
  60.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  61.         SetPort((WindowPtr) win);
  62.         GetIndString(s, kMainStrs, (bShowGray == (win->flags & kShow)) ? kSaveImageS : kSaveStereoGramS );
  63.         StandardPutFile(s, win->fs.name, &reply);
  64.         if(reply.sfGood){
  65.             modCode = -1;
  66.             errCode = noErr;
  67.             ref = -1;
  68.             if(NIL == (ph = RandomDotPicHandle(win)) || NIL == *ph || EmptyRect(&(**ph).picFrame)){
  69.                 errCode = memFullErr;
  70.             }else{
  71.                 if(NOT reply.sfReplacing){
  72.                     errCode = modCode = FSpCreate(&reply.sfFile, 'ttxt', 'PICT', reply.sfScript);
  73.                     if(noErr == errCode){ errCode = FSpOpenDF(&reply.sfFile, fsRdWrPerm, &ref); }
  74.                 }else{
  75.                     errCode = FSpGetFInfo(&reply.sfFile, &fInfo);
  76.                     if(noErr == errCode && fInfo.fdType != 'PICT'){ errCode = ePictOnly; }
  77.                     if(noErr == errCode){ errCode = FSpOpenDF(&reply.sfFile, fsRdWrPerm, &ref); }
  78.                     if(noErr == errCode){ errCode = modCode = SetEOF(ref, 0); }
  79.                 }
  80.  
  81.                 /* we've got an empty pict file.
  82.                  */
  83.                 BlockClear(s, sizeof(Str255));
  84.                 len = 256;
  85.                 if(noErr == errCode){ errCode = FSWrite(ref, &len, s); }
  86.                 if(noErr == errCode){ errCode = FSWrite(ref, &len, s); }
  87.                 if(noErr == errCode){
  88.                     len = GetHandleSize((Handle) ph);
  89.                     HLock((Handle) ph);
  90.                     errCode = FSWrite(ref, &len, *ph); 
  91.                     HUnlock((Handle) ph);
  92.                 }
  93.                 if(-1 != ref){
  94.                     FSClose(ref);
  95.                     ref = -1;
  96.                     FlushVol(NIL, reply.sfFile.vRefNum);
  97.                 }
  98.  
  99.                 /* an error occurred after the file was created/zeroed. dump the file.
  100.                  */
  101.                 if(noErr != errCode && noErr == modCode){    
  102.                     FSpDelete(&reply.sfFile);
  103.                 }
  104.                 if(noErr == errCode){
  105.                     win->fs = reply.sfFile;
  106.                     SetWTitle((WindowPtr) win, reply.sfFile.name);
  107.                 }
  108.                 KillPicture(ph);
  109.             }
  110.             TellError(errCode);
  111.         }
  112.     }
  113. }
  114.  
  115.  
  116. /* SetRandomDotMenu - 
  117.     o set the menu
  118.     o save the flags, then update them
  119.     o if they've changed, update the image to match,
  120.     o and redraw the image.
  121.  */
  122. void SetRandomDotMenu(Integer item){
  123.     static Boolean    inside = FALSE;
  124.     MenuHandle    mh;
  125.     RandomDotWindowPtr    win;
  126.     Integer        i;
  127.     LongInt        flags;
  128.     Rect        r;
  129.  
  130.     if(inside){
  131.         return;
  132.     }
  133.     inside = TRUE;
  134.     mh = GetMHandle(kRandomDotMenu);
  135.     for(i = kGrayI ; i <= kShimmerI; i++){
  136.         CheckItem(mh, i, item == i);
  137.     }
  138.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  139.         SetPort((WindowPtr) win);
  140.         flags = win->flags;
  141.         switch(item){
  142.         case kGrayI:        win->flags &= ~kShow; win->flags |= bShowGray;        break;
  143.         case kBWStereoI:    win->flags &= ~kShow; win->flags |= bShowBWStereo;    break;
  144.         case kGrayStereoI:    win->flags &= ~kShow; win->flags |= bShowGrayStereo;break;
  145.         case kShimmerI:        win->flags &= ~kShow; win->flags |= bShowGrayStereo|bIsShimmer;break;
  146.         }
  147.  
  148.         if(flags != win->flags){
  149.             switch(item){
  150.             case kGrayI:
  151.                 RandomDotPalette();
  152.                 break;
  153.             case kBWStereoI:
  154.                 RandomDotPalette();
  155.                 if(bIsGray & win->flags){
  156.                     win->flags &= ~bIsGray;
  157.                     ComputeAutoStereogram(win->grayImage, win->stereoImage, FALSE);
  158.                 }
  159.                 break;
  160.             case kGrayStereoI:
  161.             case kShimmerI:
  162.                 if(item == kGrayStereoI){
  163.                     RandomDotPalette();
  164.                 }else{
  165.                     ShimmerDotPalette();
  166.                 }
  167.                 if(NOT (bIsGray & win->flags)){
  168.                     win->flags |= bIsGray;
  169.                     ComputeAutoStereogram(win->grayImage, win->stereoImage, TRUE); 
  170.                 }
  171.                 break;
  172.             }
  173.             SetPort((WindowPtr) win);
  174.             GetContentsRect(&r);
  175.             InvalRect(&r);
  176.         }
  177.     }
  178.     inside = FALSE;
  179. }
  180.  
  181. /* DoRandomDotGrayState - 
  182.  */
  183. void DoRandomDotGrayState(void){
  184.     RandomDotWindowPtr    win;
  185.  
  186.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  187.         SetPort((WindowPtr) win);
  188.         SetRandomDotMenu(kGrayI);
  189.     }
  190. }
  191.  
  192. /* DoRandomDotBWStereoState - 
  193.  */
  194. void DoRandomDotBWStereoState(void){
  195.     RandomDotWindowPtr    win;
  196.  
  197.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  198.         SetPort((WindowPtr) win);
  199.         SetRandomDotMenu(kBWStereoI);
  200.     }
  201. }
  202.  
  203. /* DoRandomDotGrayStereoState - 
  204.  */
  205. void DoRandomDotGrayStereoState(void){
  206.     RandomDotWindowPtr    win;
  207.  
  208.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  209.         SetPort((WindowPtr) win);
  210.         SetRandomDotMenu(kGrayStereoI);
  211.     }
  212. }
  213.  
  214. /* DoRandomDotShimmer - 
  215.  */
  216. void DoRandomDotShimmer(void){
  217.     RandomDotWindowPtr    win;
  218.  
  219.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  220.         SetPort((WindowPtr) win);
  221.         SetRandomDotMenu(kShimmerI);
  222.     }
  223. }
  224.  
  225. /* DoRandomDotCut - 
  226.  */
  227. void DoRandomDotCut(void){
  228.     RandomDotWindowPtr    win;
  229.  
  230.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  231.         SetPort((WindowPtr) win);
  232.         DoRandomDotCopy();
  233.     }
  234. }
  235.  
  236. /* DoRandomDotCopy - 
  237.  */
  238. void DoRandomDotCopy(void){
  239.     OSErr                errCode;
  240.     RandomDotWindowPtr    win;
  241.     PicHandle            ph;
  242.  
  243.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  244.         SetPort((WindowPtr) win);
  245.         errCode = noErr;
  246.         if(NIL == (ph = RandomDotPicHandle(win)) || NIL == *ph || EmptyRect(&(**ph).picFrame)){
  247.             errCode = memFullErr;
  248.         }else{
  249.             ZeroScrap();
  250.             HLock((Handle) ph);
  251.             errCode = PutScrap(GetHandleSize((Handle) ph), 'PICT', (Ptr) *ph);
  252.             HUnlock((Handle) ph);
  253.             KillPicture(ph);
  254.         }
  255.         TellError(errCode);
  256.     }
  257. }
  258.  
  259. /* DoRandomDotPaste - 
  260.  */
  261. void DoRandomDotPaste(void){
  262.     RandomDotWindowPtr    win;
  263.  
  264.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  265.         SetPort((WindowPtr) win);
  266.     }
  267. }
  268.